package com.guigu.yh.springcloud.controller;

import com.guigu.yh.springcloud.common.JsonResult;
import com.guigu.yh.springcloud.entities.Payment;
import com.guigu.yh.springcloud.service.PaymentService;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * 支付控制器
 *
 * @Author 张颖辉（yh）
 * @Date 2022/8/10 19:28
 * @Version 1.0
 */
@RestController
public class PaymentController {

    @Resource
    private PaymentService paymentService;

    @Value("${server.port}")
    private String serverPort;

    @PostMapping("create")
    public JsonResult create(@RequestBody Payment payment) {
        int i = paymentService.create(payment);
        if (i > 0) {
            return new JsonResult("200", "创建成功,serverPort:" + serverPort, payment);
        }
        return new JsonResult("444", "创建失败,serverPort:" + serverPort, null);
    }

    @GetMapping("get/{id}")
    public JsonResult getPaymentById(@PathVariable("id") Long id) {
        Payment payment = paymentService.getPaymentById(id);
        if (payment != null) {
            return new JsonResult("200", "查询成功,serverPort:" + serverPort, payment);
        }
        return new JsonResult("444", "查询失败,serverPort:" + serverPort, null);
    }

    /**
     * 测试自己实现的负载均衡规则
     *
     * @return {@link String }
     * @author 张颖辉（zyh）
     * @date 2022/08/21
     * @version 1.0.0
     */
    @RequestMapping("lb")
    public String getPaymentLB() {
        return serverPort;
    }
}
